<?php

namespace app\api\controller\user;

use app\api\controller\AuthController;
use app\api\service\TaskService;

class TaskController extends AuthController
{

    /**
     * 任务统计
     * */
    public function total()
    {
        $user_id = $this->request->user['user_id'];

        $total_info = TaskService::getTaskTotalInfo($user_id);

        return json(['code'=> 1, 'msg' => '请求成功!', 'data' => $total_info]);
    }

    /**
     * 获取用户任务  任务列表
     * */
    public function index()
    {
        $task_status = $this->request->param('task_status/d',0);
        $store_id = $this->request->user['store_id'];
        $store_id  = $store_id ? $store_id:1;
        $user_id = $this->request->user['user_id'];

        $userTaskList = TaskService::getUserTaskList($user_id,$task_status, $store_id);

        return json(['code' =>1,'data' => $userTaskList, 'msg' => '获取成功!']);
    }

    /**
     * 用户领取任务
     * */
    public function receive()
    {
        $task_id = $this->request->param('task_id/d',0);

        if (empty($task_id)){
            return json(['code' =>0,'msg' => '非法参数']);
        }

        $user_id = $this->request->user['user_id'];

        $task = TaskService::findTaskDetail($task_id);

        if (empty($task)){
            return json(['code' =>0,'msg' => '领取失败,任务已结束']);
        }

        $result = TaskService::receiveTask($user_id,$task);


        return $result ? json(['code' =>1,'msg' => '任务领取成功!']) : json(['code' =>0,'msg' => '任务领取失败!']);
    }
}